package com.feihong.txgw.shopcar.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.feihong.txgw.R;
import com.feihong.txgw.shopcar.bean.TerminalEntiyt;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @name txgw_app
 * @class name：com.feihong.txgw.shopcar.adapter
 * @class describe:
 * @author wangziheng
 * @time 2017/8/5 16:02
 * @change
 * @chang time
 * @class describe
 */
public class ShopCarRecommendSecondAdapter extends BaseAdapter {

    private Context mContext;
    private List<TerminalEntiyt> mRecommendGoods;
    public ShopCarRecommendSecondAdapter(Context contenxt){
        mContext = contenxt;
        mRecommendGoods = new ArrayList<>();
    }

    public void setDatas(List<TerminalEntiyt> recommendGoodsEntities){
        mRecommendGoods = recommendGoodsEntities;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mRecommendGoods.size();
    }

    @Override
    public Object getItem(int i) {
        return mRecommendGoods.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        MyViewHolder viewHolder;
        if(convertView == null){
            viewHolder= new MyViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.shopcar_recommend_second_layout,viewGroup,false);
            viewHolder.initView(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (MyViewHolder) convertView.getTag();
        }
        viewHolder.initData(i);
        return convertView;
    }

    class MyViewHolder{
        private ImageView mRecommendGoodsImg;
        private TextView mRecommendGoodsName;
        private TextView mRecommendGoodsDescribe;
        void initView(View convertView){
            mRecommendGoodsDescribe = (TextView) convertView.findViewById(R.id.shopCar_Recommend_goods_describe);
            mRecommendGoodsImg = (ImageView) convertView.findViewById(R.id.shopCar_Recommend_goods_img);
            mRecommendGoodsName = (TextView) convertView.findViewById(R.id.shopCar_Recommend_goods_name);
        }
        void initData(int index){
            Glide.with(mContext).load(mRecommendGoods.get(index).getThumb()).into(mRecommendGoodsImg);
            mRecommendGoodsName.setText(mRecommendGoods.get(index).getSku_name());
            StringBuffer stringBuffer = new StringBuffer();
            try {
                if(!TextUtils.isEmpty(mRecommendGoods.get(index).getSpec())){
                    JSONArray jsonArray = new JSONArray(mRecommendGoods.get(index).getSpec());
                    for(int i = 0 ; i < jsonArray.length() ; i ++){
                        JSONObject jsonObject = jsonArray.optJSONObject(i);
                        stringBuffer.append(jsonObject.optString("name")+":"+jsonObject.optString("value")+"   ");
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            mRecommendGoodsDescribe.setText(stringBuffer.toString());
        }
    }
}
